#include"Tack.h"
typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
    QueueInit(&pst->q1);
    QueueInit(&pst->q2);
    return pst;
}

void myStackPush(MyStack* obj, int x) {
    if (QueueEmpty(&obj->q1) == 0)
    {
        QueuePush(&obj->q1, x);
    }
    else {
        QueuePush(&obj->q2, x);
    }
}

int myStackPop(MyStack* obj) {
    Queue* Empty = &obj->q1;
    Queue* nonEmpty = &obj->q2;
    if (QueueEmpty(&obj->q1) == 0)
    {
        Empty = &obj->q2;
        nonEmpty = &obj->q1;
    }
    while (nonEmpty->size > 1)
    {
        QueuePush(Empty, QueueFront(nonEmpty));
        QueuePop(nonEmpty);
    }
    int top = QueueFront(nonEmpty);
    QueuePop(nonEmpty);

    return top;
}

int myStackTop(MyStack* obj) {
    if (QueueEmpty(&obj->q1) == 0)
    {
        return QueueBack(&obj->q1);
    }
    else
        return QueueBack(&obj->q2);
}

bool myStackEmpty(MyStack* obj) {
    return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}

void myStackFree(MyStack* obj) {
    QueueDestroy(&obj->q1);
    QueueDestroy(&obj->q2);
    free(obj);
}
typedef struct {
    int* _a;
    int head;
    int tail;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* tmp = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    tmp->_a = (int*)malloc(sizeof(int) * (k + 1));
    tmp->head = 0;
    tmp->tail = 0;
    tmp->k = k;
    return tmp;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if ((obj->tail + 1) % (obj->k + 1) == obj->head)
        return false;
    obj->_a[obj->tail] = value;

    obj->tail = (obj->tail + 1) % (obj->k + 1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (obj->tail == obj->head)
    {
        return false;
    }
    obj->head = (obj->head + 1) % (obj->k + 1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (obj->head == obj->tail)
    {
        return -1;
    }
    return obj->_a[obj->head];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (obj->head == obj->tail)
    {
        return -1;
    }
    return obj->_a[(obj->tail + obj->k) % (obj->k + 1)];
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->head == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->tail + 1) % (obj->k + 1) == obj->head;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->_a);
    free(obj);
    obj->_a == NULL;
}
int main()
{
	Queue pt;
	QueueInit(&pt);
	QueuePush(&pt, 1);
	QueuePush(&pt, 2);
	QueuePush(&pt, 3);
	QueuePush(&pt, 4);
	printf("%d",QueueBack(&pt));
	//while (QueueEmpty(&pt)==0)
	//{
	//	printf("%d ", QueueFront(&pt));
	//	//printf("%d ", pt.size);

	//	QueuePop(&pt);
	//}
	//printf("%d", pt.size);
	printf("\n");
	QueueDestroy(&pt);
	return 0;
}